package com.hzlx;

import com.hzlx.dto.*;
import jakarta.annotation.Resource;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ProductController {
    @Resource
    RestTemplate restTemplate;
    @PostMapping("/api/product")
    public void create(@RequestBody ProductRequest request){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);//这是请求参数传递的方式为json
        HttpEntity entity = new HttpEntity(request, headers);
        restTemplate.postForLocation("http://product-service/api/product",entity);
    }
    @GetMapping("/api/product")
    public ResultDto<List<ProductResponse>> findAll(ProductRequest request){
        ParameterizedTypeReference<ResultDto<List<ProductResponse>>>  pt = new ParameterizedTypeReference<>() {};    //响应数据类型
        //远程调用
        ResponseEntity<ResultDto<List<ProductResponse>>>  exchange
                = restTemplate.exchange("http://product-service/api/product", HttpMethod.GET, HttpEntity.EMPTY, pt);
       return exchange.getBody();
    }
}
